﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ENG.TeachersDiary.Model;

namespace ENG.TeachersDiary.Classes
{
  static class XTemp
  {
    private static Dictionary<Student, Dictionary<MarkGroup, List<Mark>>> set = null;

    public static void CreateMatrix()
    {
      var db = Common.DBConn;

      set = new Dictionary<Student, Dictionary<MarkGroup, List<Mark>>>();

      var markGroups = Common.GetCurrentMarkGroups(db);
      var students = Common.GetCurrentClass(db).Students;
      int studentCount = students.Count;
      int studentIndex = 1;
      //var marks = db.Marks;

      Program.UpdateProgress(Properties.Resources.LoadingStudentsAndMarks);
      foreach (var fItem in students)
      {
        Program.UpdateProgress(Properties.Resources.LoadingMarks
          + " [" + (studentIndex++) + ". " + fItem.NameSurname + "]");
        set.Add(fItem, new Dictionary<MarkGroup, List<Mark>>());

        var ns = set[fItem];

        foreach (var ffItem in markGroups)
        {
          ns.Add(ffItem, new List<Mark>());
        } // foreach (var ffItem in markGroups)

        var orderedMarks = fItem.Marks.OrderBy(i => i.Date).OrderBy(i => i.MarkID);

        foreach (var fMark in fItem.Marks)
        {
          ns[fMark.MarkGroup].Add(fMark);
        }
      } // foreach (var fItem in students)      

      Program.UpdateProgress(Properties.Resources.LoadingMarks + " (...)");
    }

    public static StudentResultCollection Calculate()
    {
      Program.UpdateProgress(Properties.Resources.CalculatingResults);

      StudentResultCollection ret = new StudentResultCollection();

      foreach (var fStudent in set.Keys)
      {
        Program.UpdateProgress(Properties.Resources.CalculatingResultsFor + fStudent.SurnameName);

        StudentResult st = new StudentResult(fStudent);
        ret.Add(st);

        foreach (var fMarkGroup in set[fStudent].Keys)
        {
          MarkGroupResult mgr = new MarkGroupResult(fMarkGroup);
          st.Results.Add(mgr);

          foreach (var fMark in set[fStudent][fMarkGroup])
          {
            mgr.AddValue(fMarkGroup, fMark);            
          } // foreach (var kItem in set[fItem][xItem])
        } // foreach (var xItem in set[fItem])

        if (Properties.Settings.Default.ContinousSumCalc)
        {
          SumResult sr = SumResult.Create(st.Results);
          st.SumResult = sr;
        }
        else
          st.SumResult = null;

      } // foreach (var fItem in set.Values)

      return ret;
    }
  }
}
